import socket
import fcntl
import struct
import os
  
def ip(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);  
    inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]));  
    ip = socket.inet_ntoa(inet[20:24]);  
    return ip;

def mac(ifname):
    DEVICE_NAME_LEN = 15  
    MAC_START = 18  
    MAC_END = 24  
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  
    info = fcntl.ioctl(s.fileno(), 0x8927,  
                       struct.pack('256s', ifname[:DEVICE_NAME_LEN]))  
    mac = ''.join(['%02x:' % ord(char)  

                    for char in info[MAC_START:MAC_END]])[:-1]
    return mac
#if __name__ == "__main__":  
#    mac("ens32")
